home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Mosml.mlp < prev    next >
Encoding:
Text File  |  1997-08-18  |  3.0 KB  |  117 lines  |  [TEXT/R*ch]

  1. (* Mosml -- Moscow ML specific functions *)
  2.  
  3. local 
  4.     prim_val argv_   : string Vector.vector = 0 "command_line";
  5.     prim_val byte_   : real -> int -> Word8.word = 2 "get_nth_char";
  6. in
  7.  
  8. #include "../config/m.h"
  9. #ifdef MOSML_BIG_ENDIAN
  10.  
  11. fun doubleVec (r : real) =
  12.     Word8Vector.tabulate(8, fn i => byte_ r i)
  13.  
  14. #else
  15.  
  16. fun doubleVec (r : real) =
  17.     Word8Vector.tabulate(8, fn i => byte_ r (7-i))
  18.  
  19. #endif
  20.  
  21. fun floatVec (r : real) = 
  22.     let infix 5 << >> ~>> 
  23.     val dv = doubleVec r 
  24.     val eoffset = (150-23) - (1075-52)
  25.     open Word8 Word8Vector
  26.     val s = andb(sub(dv, 0), 0wx80)
  27.     val e = Int.+(Int.+(Int.*(toInt(andb(sub(dv, 0), 0wx7F)), 0x10),
  28.                 toInt(sub(dv, 1) >> 0w4)),
  29.               eoffset)
  30.     val e = if Int.<(e, 0) orelse Int.>(e, 255) then 
  31.                 raise Fail "Mosml.floatVec: float range error"
  32.         else fromInt e
  33.     val m1 = andb(sub(dv, 1), 0wxF)
  34.     val m2 = sub(dv, 2)
  35.     val m3 = sub(dv, 3)
  36.     val m4 = sub(dv, 4)
  37.     val w0 = orb(s, e >> 0w1)
  38.     val w1 = orb(e << 0w7, orb(m1 << 0w3, m2 >> 0w5))  
  39.     val w2 = orb(m2 << 0w3, m3 >> 0w5)
  40.     val w3 = orb(m3 << 0w3, m4 >> 0w5)
  41.     in 
  42.     fromList[w0,w1,w2,w3]
  43.     end
  44.  
  45. prim_val md5sum : string -> string = 1 "md5sum";
  46.  
  47. fun argv () = 
  48.     let fun h i res = 
  49.     if i >= 0 then h (i-1) (Vector.sub(argv_, i) :: res)
  50.     else res
  51.     in h (Vector.length argv_ - 1) [] end;
  52.  
  53. (* Requires Time and Timer to be loaded *)
  54.  
  55. fun time f arg =
  56.     let open Timer
  57.     val cputimer  = startCPUTimer ()
  58.     val realtimer = startRealTimer ()
  59.     fun report () = 
  60.         let val {usr, sys, gc} = checkCPUTimer cputimer;
  61.         val rea = checkRealTimer realtimer;
  62.         fun format t = Time.toString t
  63.         in TextIO.print("User: "     ^ format usr ^
  64.                 "  System: " ^ format sys ^ 
  65.                 "  GC: "     ^ format gc  ^ 
  66.                 "  Real: "   ^ format rea ^ "\n")
  67.         end
  68.     fun x before y = x
  69.     in
  70.     (f arg before report ())
  71.     handle e => (report (); raise e)
  72.     end;
  73.  
  74. fun listDir path =
  75.     let open FileSys
  76.     val dir = openDir path
  77.     fun read "" res = res
  78.       | read f  res = read (readDir dir) (f :: res)
  79.     val files = read (readDir dir) []
  80.     in closeDir dir; files end
  81.     handle exn as SysErr (msg, _) => (BasicIO.print msg; raise exn)
  82.  
  83. datatype runresult = 
  84.     Success of string
  85.   | Failure of string
  86.  
  87. fun run cmd args inp =
  88.     let fun catenate xs = 
  89.         String.concat (List.foldr (fn (s, res) => s :: " " :: res) [] xs)
  90.     fun write filename s = 
  91.         let open BinIO
  92.         val os = openOut filename
  93.         in output(os, s); closeOut os end
  94.     fun read filename = 
  95.         let open BinIO
  96.         val is  = openIn filename
  97.         val res = inputAll is
  98.         in closeIn is; res end
  99.     val infile  = FileSys.tmpName ()
  100.     val _ = write infile (Byte.stringToBytes inp)
  101.     val outfile = FileSys.tmpName ()
  102.     val cmdline = 
  103.         catenate (cmd :: List.@(args, ["<", infile, ">&", outfile]))
  104.     val status = Process.system cmdline
  105.     val result = if status = Process.success then 
  106.                      Success (Byte.bytesToString (read outfile))
  107.              else 
  108.              Failure (Byte.bytesToString (read outfile))
  109.     in 
  110.     FileSys.remove infile;
  111.     FileSys.remove outfile;
  112.     result
  113.     end
  114.  
  115. end (* local *)
  116.  
  117.